package com.xidian.zhiancloud.controller.myyb;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.SysJob;
import com.xidian.zhiancloud.service.myyb.IJobService;
import com.xidian.zhiancloud.service.myyb.ISysUserService;
import com.xidian.zhiancloud.util.tools.PageUtil;
import com.xidian.zhiancloud.util.tools.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *  系统设置-职位管理
 */
@RestController
@RequestMapping("/job")
public class JobController {

    private final static Logger logger = LoggerFactory.
            getLogger(JobController.class);

    @Autowired
    private IJobService jobService;

    @Autowired
    private ISysUserService userService;

    /**
     * 职位管理列表
     * @param params 搜索参数(职位名称)
     * @return 返回 ResultDTO 对象
     */
    @GetMapping("/job_list")
    public ResultDTO<List<SysJob>> selectJobList(@RequestParam Map<String, Object> params) {
        Query query = new Query(params);
        List<SysJob> jobList = jobService.queryList(query);
        int total = jobService.queryTotal(query);
        PageUtil pageUtil = new PageUtil(jobList, total, query.getSize(), query.getPage());
        return ResultDTO.successOf(jobList,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除职位
     * @param id 职位ID
     * @return 返回 ResultDTO 对象
     */
    @GetMapping("/deleteJob")
    public ResultDTO<Boolean> deleteJob(@RequestParam("id") Long id) {
        jobService.removeById(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 编辑职位
     * @param request 职位ID
     * @param jobs 编辑的数据
     * @return 返回 ResultDTO 对象
     */
    @PatchMapping("/updateJob")
    public ResultDTO<Boolean> updateJob(HttpServletRequest request, SysJob jobs) {
        SysJob job = jobService.selectByPrimaryKey(Long.valueOf(request.getParameter("id")));
        String postOrder = String.valueOf(jobs.getPostOrder());
        if (StringUtils.isNotBlank(postOrder)) {
            job.setPostOrder(Integer.valueOf(postOrder));
        }
        if (StringUtils.isNotBlank(jobs.getPostName())) {
            job.setPostName(jobs.getPostName());
        }
        job.setUpdateTime(new Date());
        jobService.updateByPrimaryKeySelective(job);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加职位
     * @param jobs 职位添加的数据
     * @return 返回 ResultDTO 对象
     */
    @PostMapping(value = "/addJob")
    public ResultDTO<Boolean> addJob(SysJob jobs) {
        int postOrder = jobs.getPostOrder();
        String postName = jobs.getPostName();
        SysJob job = new SysJob();
        job.setCompanyId(Long.valueOf("1"));
        job.setPostOrder(postOrder);
        job.setPostName(postName);
        job.setPostNum(Long.valueOf("0"));
        job.setPositionNum(Long.valueOf("0"));
        job.setStatus(0);
        job.setCreatorId(Long.valueOf("1"));
        job.setCreateTime(new Date());
        job.setUpdateTime(new Date());
        jobService.insert(job);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }


    /**
     * 批量删除
     * @param ids 职位数组IDs
     * @return 返回 ResultDTO 对象
     */
    @DeleteMapping("/deleteByIds")
    public ResultDTO<Boolean> deleteByIds(Long[] ids) {
        jobService.deleteByIds(ids);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }


}
